﻿@{
    Layout = null;
    var roleId = this.Context.Request.Query["ID"];
    var tableName = PageViewModel.PageTableID(this, "edit_privalige");
    var menu_operation_list_id = PageViewModel.ControlID(this, "menu_operation_list");
    var edit_privalige_layout = PageViewModel.ControlID(this, "edit_privalige_layout");


    var table_tree_load_success = PageViewModel.ControlID(this, "table_tree_load_success");
    var table_tree_node_select = PageViewModel.ControlID(this, "table_tree_node_select");
    var table_tree_node_checked = PageViewModel.ControlID(this, "table_tree_node_checked");
    var table_tree_node_checked_status = PageViewModel.ControlID(this, "table_tree_node_checked_status");
    var table_tree_node_checked_add_parents = PageViewModel.ControlID(this, "table_tree_node_checked_add_parents");
    var table_tree_node_checked_add_childrens = PageViewModel.ControlID(this, "table_tree_node_checked_add_childrens");

    var menu_operation_list_load_success = PageViewModel.ControlID(this, "menu_operation_list_load_success");
    var menu_operation_list_fmt = PageViewModel.ControlID(this, "menu_operation_list_fmt");
    var menu_operation_list_check = PageViewModel.ControlID(this, "menu_operation_list_check");
}

<div id="@edit_privalige_layout"  class="easyui-layout" data-options="fit:true">
    <script type="text/javascript">
        function @table_tree_load_success () {
            var dlg = $("#@edit_privalige_layout");
            var roleId = parseInt(dlg.find("input[name=RoleID]").val());
            if (roleId > 0) {
                dlg.find("input[name=RoleID]").val("");
                $.ajax({
                    method: 'get',
                    url: $.Content('~/Systems/GetRoleMenusByRoleID/' + roleId),
                    context: this,
                    success: function (e) {
                        for (var i = 0; i < e.length; i++) {
                            if (e[i].Status === 1) {
                                try {
                                    $(this).treegrid("checkNode", e[i].MenuID);
                                } catch (ex) { ; }
                            }
                        }
                        dlg.find("input[name=RoleID]").val(roleId);
                    },
                    complete: function () {
                        dlg.find("input[name=RoleID]").val(roleId);
                    }
                });
            }
        }

        function @table_tree_node_select (row) {
            var dlg = $("#@edit_privalige_layout");
            dlg.find('#@menu_operation_list_id').datalist('reload', '~/Systems/GetAppMenuOperations/' + row.Id);
        }

        function @table_tree_node_checked (row) {
            var dlg = $("#@edit_privalige_layout");
            var roleId = parseInt(dlg.find("input[name=RoleID]").val());

            if (roleId > 0) {
                $(this).treegrid('select', row.Id);
                var list = new Array();
                var item = {
                    MenuID: row.Id,
                    RoleID: roleId,
                    Status: @table_tree_node_checked_status (row)
                };
                list.push(item);

                @table_tree_node_checked_add_parents (row, list, item.RoleID);
                @table_tree_node_checked_add_childrens (row, list, item.RoleID);

                $.ajax({
                    async: false,
                    method: 'post',
                    contentType: "application/json",
                    url: "~/Systems/UpdateRoleMenu",
                    data: JSON.stringify(list),
                    success: function (e) {
                        @table_tree_node_select (row);
                    },
                    error: function (e) {
                        ShowAjaxError(e);
                    }
                });
            }
        }

        function @table_tree_node_checked_add_parents (node, list, roleId) {
            var pid = node.ParentID;
            while (pid) {
                var pnode = $("#@edit_privalige_layout").find("#@tableName").treegrid("find", pid);
                if (pnode === null) { break; }
                list.push({
                    MenuID: pnode.Id,
                    RoleID: roleId,
                    Status: @table_tree_node_checked_status (pnode)
                });

                pid = pnode.ParentID;
            }
        }

        function @table_tree_node_checked_add_childrens (node, list, roleId) {
            if (node && node.children) {
                for (var i = 0; i < node.children.length; i++) {
                    var n = node.children[i];
                    list.push({
                        MenuID: n.Id,
                        RoleID: roleId,
                        Status: @table_tree_node_checked_status (n)
                    });
                    @table_tree_node_checked_add_childrens (n, list, roleId);
                }
            }
        }

        function @table_tree_node_checked_status (n) {
            if (n) { return (n.checked ? 1 : (n.checkState === "indeterminate" ? 2 : 0)); }
            return 0;
        }
    </script>

    <script type="text/javascript">
        function @menu_operation_list_load_success (args) {
            var dlg = $("#@edit_privalige_layout");
            if (args.rows.length) {
                var roleId = parseInt(dlg.find("input[name=RoleID]").val());
                dlg.find("input[name=ActionLoaded]").val("");
                $.ajax({
                    method: 'get',
                    url: '~/Systems/GetRoleMenuOperations?roleId=' + roleId + "&menuId=" + args.rows[0].MenuID,
                    success: function (e) {
                        var list = dlg.find("#@menu_operation_list_id");
                        var rows = list.datalist("getRows");
                        for (var i = 0; i < e.length; i++) {
                            if (e[i].Enable) {
                                for (var j = 0; j < rows.length; j++) {
                                    if (rows[j].OperationID === e[i].OperationID) {
                                        list.datalist("checkRow", j);
                                        break;
                                    }
                                }
                            }
                        }
                    },
                    complete: function () {
                        dlg.find("input[name=ActionLoaded]").val("true");
                    }
                });
            }
        }

        function @menu_operation_list_check (index, row, checked) {
            var dlg = $("#@edit_privalige_layout");
            if (dlg.find("input[name=ActionLoaded]").val()) {
                row.RoleID = parseInt(dlg.find("input[name=RoleID]").val());
                row.Enable = checked;

                $.ajax({
                    method: 'post',
                    url: '~/Systems/UpdateRoleMenuOperation',
                    data: row,
                    success: function (e) {
                        if (!e.Result) { $.messager.alert('失败', e.Msg, 'error'); }
                    },
                    error: function (e) {
                        ShowAjaxError(e);
                    }
                });
            }
        }
    </script>
    <script type="text/javascript">
        function @menu_operation_list_fmt (v, r, i) {
            var op = GetObject(r.OperationID, '~/Systems/GetAppOperationByID');
            if (op != null) {
                var str = "<div style='display:flex;justify-content:space-between;padding-left:10px;padding-right:10px'>";
                str += op.Name;
                str += "</span>";
                str += ("<span style='font-size:12px'>" + op.Code + "</span>");
                str += "</div>";

                return str;
            }
        }
    </script>

    <div data-options="region:'west',split:true,border:false,collapsible:false" style="width:70%">
        <input name="RoleID" type="hidden" value="@roleId" />
        <table id="@tableName" class="easyui-treegrid" fit="true" fitColumns="true"
               data-options="rownumbers:true,checkbox:true,border:false,idField:'Id',treeField:'Name',method:'get',url:'~/Systems/GetAppMenuTree',
                                   onSelect:@table_tree_node_select,
                                   onLoadSuccess:@table_tree_load_success,
                                   onCheckNode:@table_tree_node_checked">
            <thead>
                <tr>
                    <th data-options="field:'Code'" width="60">编号</th>
                    <th data-options="field:'Name'" width="120">名称</th>
                    <th data-options="field:'URL'" width="160">路径</th>
                </tr>
            </thead>
        </table>
    </div>
    <div data-options="region:'center',border:false" style="width:30%">
        <input name="ActionLoaded" type="hidden" />
        <div class="easyui-tabs" data-options="justified:false,border:false" style="width:100%" fit="true">
            <div title="功能权限">
                <ul id="@menu_operation_list_id" class="easyui-datalist" fit="true"
                    data-options="rownumbers:true,checkbox:true,border:false,method:'get',lines:true,singleSelect:false,checkOnSelect:true,selectOnCheck:true,
                             valueField:'Id',textField:'Name',
                             onLoadSuccess:@menu_operation_list_load_success,
                             onCheck:function(index, row){ @menu_operation_list_check (index, row, true); },
                             onUncheck:function(index, row){ @menu_operation_list_check (index, row, false); },
                             textFormatter:@menu_operation_list_fmt"></ul>
            </div>
            @*<div title="数据权限"></div>
            <div title="字段权限"></div>*@
        </div>
    </div>
</div>
